//
//  SettingViewController.m
//  zhuchiren
//
//  Created by rainday on 15/1/11.
//  Copyright (c) 2015年 rainday. All rights reserved.
//

#import "SettingViewController.h"
#import "ModifyAvatarViewController.h"
#import "ModifySignViewController.h"
#import "ModifyNickNameViewController.h"
#import "ModifyPassWordViewController.h"
#import <ReactiveCocoa.h>
#import <AFNetworking.h>
#import <UIImageView+AFNetworking.h>

#define LIST @[@[@"头像",@"昵称",@"手机号",@"性别",@"身高"],@[@"密码",@"签名"]]
#define DEMODATA @[@[@"",@"朕是皇上",@"123xxx89023",@"男",@"180"],@[@"修改",@"暂无"]]


@interface SettingViewController ()<UITableViewDelegate,UITableViewDataSource,ModifyAvatarDelegate,ModifyNickNameViewControllerDelegate,ModifyPassWordViewControllerDelegate,ModifySignViewControllerDelegate>


@property (nonatomic ,weak) IBOutlet UIView *navView;
@property (nonatomic ,weak) IBOutlet UIButton *dismissButton;
@property (nonatomic ,weak) IBOutlet UITableView *tableView;


- (IBAction)dismissSelf:(id)sender;
@end


static NSString *reuseIdentify = @"cell";
@implementation SettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    _navView.layer.shadowColor = [[UIColor colorWithRed:224.0/255 green:79.0/255 blue:95.0/255 alpha:1.0] CGColor];
    _navView.layer.shadowOffset = CGSizeMake(1.0, 1.0);
    _navView.layer.shadowOpacity = .3;
    
    
    [_tableView setBackgroundColor:[UIColor clearColor]];
    
    
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)dismissSelf:(id)sender{
    [_delegate settingViewController:self hasFinished:YES];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [LIST[section] count];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return [LIST count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return indexPath.section == 0  && indexPath.row == 0
    ? 60:30;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 15;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 15)];
    [view setBackgroundColor:[UIColor clearColor]];
    
    return view;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentify];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseIdentify];
        
    }
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.text = LIST[indexPath.section][indexPath.row];
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    
    cell.detailTextLabel.font = [UIFont systemFontOfSize:12];
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    
  
    
    NSString *detailString =@"";
    if (0 == indexPath.section) {
        switch (indexPath.row) {
            case 0:{
                break;
            }
                
            case 1:
                
                detailString = [userDefaults objectForKey:@"nickname"];
                break;
            case 2:
                detailString = [userDefaults objectForKey:@"phone"];
                break;
            case 3:
                detailString = [userDefaults objectForKey:@"gender"];
                break;
            case 4:
                detailString = [userDefaults objectForKey:@"height"];
                break;
            default:
                break;
        }
        
    }
    
    if (1== indexPath.section) {
        switch (indexPath.row) {
            case 0:
                detailString = @"修改";
                break;
            case 1:
                detailString = @"修改";
                break;
            default:
                break;
        }
    }
    cell.detailTextLabel.text = detailString;

   
    
    
    if ( 0 == indexPath.section && indexPath.row >1) {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    if (0== indexPath.section && 0== indexPath.row) {
        UIImageView *avatarImageView = [[UIImageView alloc] initWithFrame:CGRectMake(tableView.frame.size.width-80, 10, 40, 40)];
        avatarImageView.layer.masksToBounds = YES;
        avatarImageView.layer.cornerRadius = 5;
        
        NSString *avatarString = [userDefaults objectForKey:@"avatar"];
        
        [avatarImageView setImageWithURL:[NSURL URLWithString:avatarString] placeholderImage:[UIImage imageNamed:@"touxiang.jpg"]];
        
            
       
        
        [cell addSubview:avatarImageView];
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (0 == indexPath.section && 0 == indexPath.row) {
        ModifyAvatarViewController *modifyAvatarVC = [[ModifyAvatarViewController alloc] initWithNibName:@"ModifyAvatarViewController" bundle:nil];
        modifyAvatarVC.delegate = self;
        [self presentViewController:modifyAvatarVC animated:YES completion:nil];
    }
    
    
    if (0 == indexPath.section && 1 == indexPath.row) {
        ModifyNickNameViewController *modifyNickNameVC = [[ModifyNickNameViewController alloc] initWithNibName:@"ModifyNickNameViewController" bundle:nil];
        ((ModifyNickNameViewController *)modifyNickNameVC).delegate = self;
        
        [self presentViewController:modifyNickNameVC animated:YES completion:nil];
    }
    
    if (1 == indexPath.section && 0 == indexPath.row) {
        ModifyPassWordViewController *modifyPassWordVC = [[ModifyPassWordViewController alloc] initWithNibName:@"ModifyPassWordViewController" bundle:nil];
        ((ModifyPassWordViewController *)modifyPassWordVC).delegate = self;
        [self presentViewController:modifyPassWordVC animated:YES completion:nil];
        
    }
    
    if (1 == indexPath.section && 1 == indexPath.row) {
        ModifySignViewController *modifySignVC = [[ModifySignViewController alloc] initWithNibName:@"ModifySignViewController" bundle:nil];
        ((ModifySignViewController *)modifySignVC).delegate = self;
        [self presentViewController:modifySignVC animated:YES completion:nil];
    }
    
    
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


- (void)avatarViewController:(ModifyAvatarViewController *)avatarViewController hasFinishedAvatar:(NSString *)avatar{
    [avatarViewController dismissViewControllerAnimated:YES completion:nil];
    [self.tableView reloadData];
}

- (void)modifyNickNameViewController:(ModifyNickNameViewController *)modifyViewController hasFinished:(BOOL)hasFinished{
    [modifyViewController dismissViewControllerAnimated:YES completion:nil];
    [self.tableView reloadData];
}

- (void)passWordViewController:(ModifyPassWordViewController *)passwordViewController hasFinishedUpdate:(BOOL)hasFinishedUpdate{
    [passwordViewController dismissViewControllerAnimated:YES completion:nil];
}

- (void)singViewController:(ModifySignViewController *)signViewController hasFinishedUpdate:(BOOL)hasFinishedUpdate{
    [signViewController dismissViewControllerAnimated:YES completion:nil];
}
@end
